<?php
# Interface for the RASE client library.
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_once dirname(__FILE__) . '/../Raws.php';
require_once dirname(__FILE__) . '/../Util.php';
require_once dirname(__FILE__) . '/Rase/WowsdpEntry.php';
require_once dirname(__FILE__) . '/Rase/WowsdpFeed.php';
require_once dirname(__FILE__) . '/Rase/WowsdpQuery.php';
require_once dirname(__FILE__) . '/Rase/WowappEntry.php';
require_once dirname(__FILE__) . '/Rase/WowappFeed.php';
require_once dirname(__FILE__) . '/Rase/WowappQuery.php';
require_once dirname(__FILE__) . '/Rase/MpegtsEntry.php';
require_once dirname(__FILE__) . '/Rase/MpegtsFeed.php';
require_once dirname(__FILE__) . '/Rase/MpegtsQuery.php';

class Rase extends Raws
{
    public static $namespaces = array(
            'raws' => 'http://rambla.be/raws/ns-metadata/1.0');

    public function __construct($user = null, $pwd = null, $base_uri = null, $path = null, $client = null, $applicationId = 'MyCompany-MyApp-1.0', $ssl = False)
    {
      if (! $base_uri) {
        $base_uri = "http://rase.cdn01.rambla.be";
      }
      $this->registerPackage('Rase');
      $this->registerPackage('Rase_Extension');
      parent::__construct($user, $pwd, $base_uri, $path, $client, $applicationId, $ssl);
    }

    /**
     * Retrieve the feed for a RASE Wowsdp instance.
     *
     * @param string $path The relative path to the directory.
     * @param string $query An object of type WowsdpQuery, or null for no query-string.
     * @return Rase DirFeed object
     */
    public function getWowsdpFeed($path = null, $query = null)
    {
      $resource = "wowsdp";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), null, $path);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource, $path);
      }
      return parent::getFeed($uri, 'Rase_WowsdpFeed');
    }

    /**
     * Retrieve the feed for a RASE Wowapp instance.
     *
     * @param string $path The relative path to the directory.
     * @param string $query An object of type WowappQuery, or null for no query-string.
     * @return Rase DirFeed object
     */
    public function getWowappFeed($path = null, $query = null)
    {
      $resource = "wowapp";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), null, $path);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource, $path);
      }
      return parent::getFeed($uri, 'Rase_WowappFeed');
    }
    
    /**
     * Retrieve the feed for a RASE Mpegts instance.
     *
     * @param string $path The relative path to the directory.
     * @param string $query An object of type MpegtsQuery, or null for no query-string.
     * @return Rase DirFeed object
     */
    public function getMpegtsFeed($path = null, $query = null)
    {
      $resource = "mpegts";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), null, $path);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource, $path);
      }
      return parent::getFeed($uri, 'Rase_MpegtsFeed');
    }

    # Create Wowza Application (future use): this functionality is not yet enabled in production (set db_only to 1 for apps < 10/2011).
    public function createWowapp($username, $stream_type, $db_only = 0)
    {
      # URI to post to
      $uri = safe_raws_uri($this->getBaseUri(), "wowapp");
      // Create a new entry object (short notation for: $newEntry = new Rats_InputEntry();)
      $newEntry = $this->newWowappEntry();
      // Create a new wowappParams object (short notation for: $newParams = new Rats_Extension_Params();)
      $newParams = $this->newWowappParams();
      $newParams->username = $this->newUsername($username);
      $newParams->stream_type = $this->newStreamType($stream_type);
      $newParams->db_only = $this->newDbOnly($db_only);

      # Create a new inputContent object and set it on the entry
      $newEntry->content = $this->newWowappContent($newParams);
      # Call app's insertEntry() to send it to the server
      return $this->insertEntry($newEntry, $uri, 'Rase_WowappEntry');
    }
    
    # Delete Wowza Application (future use): this functionality is not yet enabled in production.
    public function deleteWowapp($username, $stream_type)
    {
      # URI to post to
      $uri = safe_raws_uri($this->getBaseUri(), "delete_wowapp");
      // Create a new entry object (short notation for: $newEntry = new Rats_InputEntry();)
      $newEntry = $this->newWowappEntry();
      // Create a new wowappParams object (short notation for: $newParams = new Rats_Extension_Params();)
      $newParams = $this->newWowappParams();
      $newParams->username = $this->newUsername($username);
      $newParams->stream_type = $this->newStreamType($stream_type);

      # Create a new inputContent object and set it on the entry
      $newEntry->content = $this->newWowappContent($newParams);
      # Call app's insertEntry() to send it to the server
      return $this->insertEntry($newEntry, $uri, 'Rase_WowappEntry');
    }
    
    # This functionality is not yet enabled in production.
    public function deleteUser($username, $cdn)
    {
      # URI to delete
      $uri = safe_raws_uri($this->getBaseUri(), "delete_user", $username . "/" . $cdn);
      $this->delete($uri);
    }

    # This functionality is not yet enabled in production.
    public function getUserWowapps($user)
    {
      $uri = safe_raws_uri($this->getBaseUri(), "user_wowapp", $user);
      return parent::getFeed($uri, 'Rase_WowappFeed');
    }
    
    public function createUser($username)
    {
      $client = $this->getHttpClient();
      # URI to post to
      $uri = safe_raws_uri($this->getBaseUri(), "user", $username);
      $client->setUri($uri);
      
      $response = $client->request('POST');
      if (($response->getStatus() != 404) and (!$response->isSuccessful())) {
          require_once 'Zend/Gdata/App/HttpException.php';
          $exception = new Zend_Gdata_App_HttpException('Expected response code 200 or 404, got ' . $response->getStatus());
          $exception->setResponse($response);
          throw $exception;
      }
      if ($response->getStatus() != 200) {
        return null;
      }
      return $response;
    }


}
